package cn.wolfcode.web.controller;


import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.web.feign.AlipayFeignApi;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;


@RestController
@RequestMapping("/orderPay")
@RefreshScope
public class OrderPayController {
    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private AlipayFeignApi alipayFeignApi;
    @Value("${pay.frontEndPayUrl}")
    private String frontEndPayUrl;

    /**
     * 同步回调方法
     *
     * @return 最终需要跳转到订单详情界面
     */
    @RequestMapping("/return_url")
    public void return_url(@RequestParam Map<String, String> params, HttpServletResponse response) throws IOException {
        //验签操作
        //boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type); //调用SDK验证签名
        Result<Boolean> result = alipayFeignApi.rsaCheck(params);
        if (result != null && !result.hasError()) {
            //验签成功 重定向到订单详情界面
            if (result.getData()) {
                //response.sendRedirect("/order_detail.html?orderNo=" + params.get("out_trade_no"));
                response.sendRedirect(frontEndPayUrl + params.get("out_trade_no"));

            } else {
                //验签失败 跳转到错误界面中
                response.sendRedirect("/error.html test");
            }
        }
    }

    /**
     * 异步回调方法
     *
     * @return
     */
    @RequestMapping("notify_url")
    public void notify_url(@RequestParam Map<String, String> params, HttpServletResponse response) throws IOException {
        Result<Boolean> result = alipayFeignApi.rsaCheck(params);
        PrintWriter out = response.getWriter();
        if (result != null && !result.hasError()) {
            //验签成功
            if (result.getData()) {
                String orderNo = params.get("out_trade_no");
                orderInfoService.paySuccess(orderNo);
                out.write("success");
            } else {
                out.write("fail");
            }
        }
    }

    @RequestMapping("/refund")
    public Result<String> refund(String orderNo) {
        //参数校验
        if (StringUtils.isBlank(orderNo)) {
            throw new BusinessException(SeckillCodeMsg.PARAM_ERROR);
        }
        //查询订单信息,根据支付类型作不同类型退款
        OrderInfo orderInfo = orderInfoService.find(orderNo);
        if (OrderInfo.PAYTYPE_ONLINE == orderInfo.getPayType()) {
            //走线上退款流程
            orderInfoService.refundOnline(orderInfo);
            return Result.success("");
        } else if (OrderInfo.PAYTYPE_INTERGRAL == orderInfo.getPayType()) {
            //走积分退款流程
            orderInfoService.refundIntergarl(orderInfo);
            return Result.success("");
        } else {
            throw new BusinessException(SeckillCodeMsg.PARAM_ERROR);
        }
    }

    @RequestMapping("/alipay")
    public Result<String> alipay(String orderNo, int type) {
        //参数校验
        if (StringUtils.isBlank(orderNo) || StringUtils.isBlank(String.valueOf(type))) {
            throw new BusinessException(SeckillCodeMsg.PARAM_ERROR);
        }
        //判断支付类型
        if (OrderInfo.PAYTYPE_ONLINE == type) {
            //走线上支付流程
            String formString = orderInfoService.payOnline(orderNo);
            return Result.success(formString);
        } else if (OrderInfo.PAYTYPE_INTERGRAL == type) {
            //走积分支付流程
            orderInfoService.payIntergral(orderNo);
        } else {
            throw new BusinessException(SeckillCodeMsg.PARAM_ERROR);
        }
        return Result.success("");
    }
}
